<?php

namespace App\Http\Controllers;

use App\Http\Model\ImgModel;
use Illuminate\Http\Request;

class UploadController extends Controller
{
    public function moveImg($fileName){
        $path = "../storage/app/public".date("/Y/m/d/");

        $tmp_path = "../storage/app/";

        $save_file = explode("/",$fileName)[1];

        if(!file_exists($path)){
            mkdir($path,0777,true);
        }

        copy($tmp_path.$fileName,$path.$save_file);

        unlink($tmp_path.$fileName);

        return $this->uuid();
    }

    public function upload(Request $req){
        $savePath = date("/Y/m/d");

        $p = $req->file("file")->store("tmp");

        return response()->json($p);
    }

    public function uuid(){

        mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.

        $charid = strtoupper(md5(uniqid(rand(), true)));

        return $charid;
    }

    public function getImg($uuid){
        $im = new ImgModel();

        $file = $im->select("file")->where("imgId",$uuid)->first();

        return response()->json($file);
    }
}
